											
** ---------------------------------- **											
* Author: Abdulrazzak Tamim 											
* First version: 16/10/2019											
* Last modified: 26/4/2024							
** ---------------------------------- **											
											
* User must define a global macro in order to run the program:												
* "path" contains the raw data												
												
												
clear all												
set more off												
program drop _all												
												
if "`c(username)'"=="abdulrazzaktamim" {												
gl path "/Users/abdulrazzaktamim/Library/CloudStorage/Dropbox/SoilDoc-Abdulrazzak"									
//adopath ++ "$path/ANALYSIS/programs/depends"												
									
}
	
program main	
	
	use "$path/replication/data/fertilizer.dta", clear
	merge m:1 respondent_id using "$path/replication/data/controls.dta", /// 
	nogen keepusing(headage malehead headeduc headeducp hhsize asstfullsc_qrt)
	
	mat A_6 = J(5,1,.) 
	mat A_9 = J(5,1,.) 
	mat B_6 = J(5,1,.) 
	mat B_9 = J(5,1,.) 
	mat C_6 = J(5,1,.) 
	mat C_9 = J(5,1,.) 
	mat D_6 = J(5,1,.) 
	mat D_9 = J(5,1,.) 
	mat E_6 = J(5,1,.) 
	mat E_9 = J(5,1,.) 
	mat F_6 = J(5,1,.) 
	mat F_9 = J(5,1,.)
	
	preserve 
		drop if trt==5
		IPW
		ITT_Main
		ITT_IPW
		ITT_Spec2
		pooling
		HeterogeneityEC_IPW
	restore
	CrossVillageITT

end

program IPW 

	g missing=. 
	
	forv i=4/9 { 
		if inlist(5,7,8,`i') continue
		g missing_201`i' = (mi(fert_kg_acre_) | mi(fert_kgacre_baseline) | mi(fert_gps_acre_) | mi(fert_gps_baseline)) & year==201`i' if year==201`i'
		replace missing=missing_201`i' if year==201`i'
	}

	ta missing_2014, m 
	ta missing_2016, m 
	ta missing_2019, m 
	ta missing, m 
	
	probit missing i.trt##(c.headage i.malehead i.headeducp c.hhsize i.asstfullsc_qrt) i.village if year==2016, r cl(village) asis
	outreg2 using "$path/replication/output/TableA6.doc", replace bd(2) sd(2) nocons addtext(Village FE, YES) /// 
	label ctitle(fert16) keep(2.trt##c.headage 2.trt##1.malehead 2.trt##1.headeducp 2.trt##c.hhsize 2.trt##2.asstfullsc_qrt 2.trt##3.asstfullsc_qrt 2.trt##4.asstfullsc_qrt 3.trt##c.headage 3.trt##1.malehead 3.trt##1.headeducp 3.trt##c.hhsize 3.trt##2.asstfullsc_qrt 3.trt##3.asstfullsc_qrt 3.trt##4.asstfullsc_qrt 4.trt##c.headage 4.trt##1.malehead 4.trt##1.headeducp 4.trt##c.hhsize 4.trt##2.asstfullsc_qrt 4.trt##3.asstfullsc_qrt 4.trt##4.asstfullsc_qrt)

	predict probhat_2016
	as inrange(probhat_2016,0,1) if missing_2016!=.
	gen ipw_2016 = 1/probhat_2016
	
	probit missing i.trt##(c.headage i.malehead i.headeducp c.hhsize i.asstfullsc_qrt) i.village if year==2019, r cl(village) asis
	outreg2 using "$path/replication/output/TableA6.doc", append bd(2) sd(2) nocons addtext(Village FE, YES) label ctitle(fert19) /// 
	keep(2.trt##c.headage 2.trt##1.malehead 2.trt##1.headeducp 2.trt##c.hhsize 2.trt##2.asstfullsc_qrt 2.trt##3.asstfullsc_qrt 2.trt##4.asstfullsc_qrt 3.trt##c.headage 3.trt##1.malehead 3.trt##1.headeducp 3.trt##c.hhsize 3.trt##2.asstfullsc_qrt 3.trt##3.asstfullsc_qrt 3.trt##4.asstfullsc_qrt 4.trt##c.headage 4.trt##1.malehead 4.trt##1.headeducp 4.trt##c.hhsize 4.trt##2.asstfullsc_qrt 4.trt##3.asstfullsc_qrt 4.trt##4.asstfullsc_qrt)
	predict probhat_2019
	as inrange(probhat_2019,0,1) if missing_2019!=.
	gen ipw_2019 = 1/probhat_2019
	
	g ipw=ipw_2016 if year==2016
	replace ipw=ipw_2019 if year==2019
	
	ta ipw

end


program ITT_Main
	
	forval i=6/9 {
	if inlist(`i', 7, 8) continue
	areg fert_kg_acre_ i.trt2 fert_kgacre_baseline if year==201`i' & /// 
	fert_gps_acre_!=. &fert_gps_baseline!=., a(village) robust cl(vil) 
			outreg2 using "$path/replication/output/TableA10.doc", append ///
			ctitle(`i' kgacre) bd(2) sd(2) nocons addtext(Village FE, YES)
		su fert_kgacre_baseline if e(sample) & trt2==1
		mat A_`i'[1,1] = r(mean)
		mat A_`i'[2,1] = r(sd)
		test 2.trt2=3.trt2
		mat A_`i'[3,1] = r(p)  
		test 2.trt2=4.trt2
		mat A_`i'[4,1] = r(p)
		test 3.trt2=4.trt2
		mat A_`i'[5,1] = r(p)
		mat list A_`i'
		matmap A_`i'  AN_`i', map(round(@,0.01))
		mat list AN_`i'
		
	areg fert_gps_acre_ i.trt2 fert_gps_baseline if year==201`i' & /// 
		fert_kg_acre_!=. &fert_kgacre_baseline!=., a(village) robust cl(vil) 
			outreg2 using "$path/replication/output/TableA10.doc", append ///
			ctitle(`i' kggps) bd(2) sd(2) nocons addtext(Village FE, YES)
	    su fert_gps_baseline if e(sample) & trt2==1
		mat B_`i'[1,1] = r(mean)
		mat B_`i'[2,1] = r(sd)
		test 2.trt2=3.trt2
		mat B_`i'[3,1] = r(p)  
		test 2.trt2=4.trt2
		mat B_`i'[4,1] = r(p)
		test 3.trt2=4.trt2
		mat B_`i'[5,1] = r(p)
		mat list B_`i'
		matmap B_`i'  BN_`i', map(round(@,0.01))
		mat list BN_`i'		

	areg fert_dum_ i.trt2  fert_dum_baseline if year==201`i' & e(sample), a(village) robust cl(vil) 
			outreg2 using "$path/replication/output/TableA10.doc", append ///
			ctitle(`i' dum) bd(2) sd(2) nocons addtext(Village FE, YES)	
		su fert_dum_ if e(sample) & trt2==1
		mat C_`i'[1,1] = r(mean)
		mat C_`i'[2,1] = r(sd)
		test 2.trt2=3.trt2
		mat C_`i'[3,1] = r(p)  
		test 2.trt2=4.trt2
		mat C_`i'[4,1] = r(p)
		test 3.trt2=4.trt2
		mat C_`i'[5,1] = r(p)
		mat list C_`i'
		matmap C_`i'  CN_`i', map(round(@,0.01))
		mat list CN_`i'	
	}
	erase "$path/replication/output/TableA10.txt"
	
	*Export											
	matmap A_6 A_6N, map(round(@,0.01))											
	matmap A_9 A_9N, map(round(@,0.01))											
	matmap B_6 B_6N, map(round(@,0.01))											
	matmap B_9 B_9N, map(round(@,0.01))											
	matmap C_6 C_6N, map(round(@,0.01))											
	matmap C_9 C_9N, map(round(@,0.01))											
	
	putexcel set "$path/replication/output/TableA10_F_tests.xls", sheet("F-tests") replace	
	putexcel B1 = "fert_kg_acre_16"
	putexcel C1 = "fert_gps_acre_16"
	putexcel D1 = "fert_dum_16"
	putexcel E1 = "fert_kg_acre_19"
	putexcel F1 = "fert_gps_acre_19"
	putexcel G1 = "fert_dum_19"
	putexcel A2 = "CONTROL MEAN"
	putexcel A3 = "CONTROL SD"
	putexcel A4 = "V_vs_R"
	putexcel A5 = "V_vs_RV"
	putexcel A6 = "R_vs_RV"
	
	putexcel B2=matrix(AN_6) 											
	putexcel C2=matrix(BN_6)											
	putexcel D2=matrix(CN_6) 											
	putexcel E2=matrix(AN_9) 											
	putexcel F2=matrix(BN_9)											
	putexcel G2=matrix(CN_9) 

end		


program ITT_IPW
	
	forval i=6/9 {
	if inlist(`i', 7, 8) continue
	areg fert_kg_acre_ i.trt2 fert_kgacre_baseline if year==201`i' & /// 
	fert_gps_acre_!=. &fert_gps_baseline!=. [pw=ipw], a(village) robust cl(vil) 
			outreg2 using "$path/replication/output/Table4.doc", append ///
			ctitle(`i' kgacre) bd(2) sd(2) nocons addtext(Village FE, YES)
		su fert_kgacre_baseline if e(sample) & trt2==1
		mat A_`i'[1,1] = r(mean)
		mat A_`i'[2,1] = r(sd)
		test 2.trt2=3.trt2
		mat A_`i'[3,1] = r(p)  
		test 2.trt2=4.trt2
		mat A_`i'[4,1] = r(p)
		test 3.trt2=4.trt2
		mat A_`i'[5,1] = r(p)
		mat list A_`i'
		matmap A_`i'  AN_`i', map(round(@,0.01))
		mat list AN_`i'
		
	areg fert_gps_acre_ i.trt2 fert_gps_baseline if year==201`i' & /// 
		fert_kg_acre_!=. &fert_kgacre_baseline!=. [pw=ipw], a(village) robust cl(vil) 
			outreg2 using "$path/replication/output/Table4.doc", append ///
			ctitle(`i' kggps) bd(2) sd(2) nocons addtext(Village FE, YES)
	    su fert_gps_baseline if e(sample) & trt2==1
		mat B_`i'[1,1] = r(mean)
		mat B_`i'[2,1] = r(sd)
		test 2.trt2=3.trt2
		mat B_`i'[3,1] = r(p)  
		test 2.trt2=4.trt2
		mat B_`i'[4,1] = r(p)
		test 3.trt2=4.trt2
		mat B_`i'[5,1] = r(p)
		mat list B_`i'
		matmap B_`i'  BN_`i', map(round(@,0.01))
		mat list BN_`i'		

	areg fert_dum_ i.trt2  fert_dum_baseline if year==201`i'  & e(sample) [pw=ipw], a(village) robust cl(vil) 
			outreg2 using "$path/replication/output/Table4.doc", append ///
			ctitle(`i' dum) bd(2) sd(2) nocons addtext(Village FE, YES)	
		su fert_dum_ if e(sample) & trt2==1
		mat C_`i'[1,1] = r(mean)
		mat C_`i'[2,1] = r(sd)
		test 2.trt2=3.trt2
		mat C_`i'[3,1] = r(p)  
		test 2.trt2=4.trt2
		mat C_`i'[4,1] = r(p)
		test 3.trt2=4.trt2
		mat C_`i'[5,1] = r(p)
		mat list C_`i'
		matmap C_`i'  CN_`i', map(round(@,0.01))
		mat list CN_`i'	
	}
	erase "$path/replication/output/Table4.txt"
	
	*Export											
	matmap A_6 A_6N, map(round(@,0.01))											
	matmap A_9 A_9N, map(round(@,0.01))											
	matmap B_6 B_6N, map(round(@,0.01))											
	matmap B_9 B_9N, map(round(@,0.01))											
	matmap C_6 C_6N, map(round(@,0.01))											
	matmap C_9 C_9N, map(round(@,0.01))											
	
	putexcel set "$path/replication/output/Table4_F_tests.xls", sheet("F-tests") replace											
	putexcel B1 = "fert_kg_acre_16"
	putexcel C1 = "fert_gps_acre_16"
	putexcel D1 = "fert_dum_16"
	putexcel E1 = "fert_kg_acre_19"
	putexcel F1 = "fert_gps_acre_19"
	putexcel G1 = "fert_dum_19"
	putexcel A2 = "CONTROL MEAN"
	putexcel A3 = "CONTROL SD"
	putexcel A4 = "V_vs_R"
	putexcel A5 = "V_vs_RV"
	putexcel A6 = "R_vs_RV"
	
	putexcel B2=matrix(AN_6) 											
	putexcel C2=matrix(BN_6)											
	putexcel D2=matrix(CN_6) 											
	putexcel E2=matrix(AN_9) 											
	putexcel F2=matrix(BN_9)											
	putexcel G2=matrix(CN_9) 

end		


program ITT_Spec2

	g V=trt2==2|trt2==4 if trt2!=.
	g R=trt2==3|trt2==4 if trt2!=.
	
	forval i=6/9 {
	if inlist(`i', 7, 8) continue
	areg fert_kg_acre_ i.V##i.R fert_kgacre_baseline if year==201`i' & /// 
	fert_gps_acre_!=. &fert_gps_baseline!=., a(village) robust cl(vil) 
			outreg2 using "$path/replication/output/TableA13.doc", append ///
			ctitle(`i' kgacre) bd(2) sd(2) nocons addtext(Village FE, YES) /// 
			keep(1.V 1.R 1.V#1.R fert_kgacre_baseline)
		su fert_kgacre_baseline if e(sample) & trt2==1
		mat A_`i'[1,1] = r(mean)
		mat A_`i'[2,1] = r(sd)
		test 1.V=1.R
		mat A_`i'[3,1] = r(p)  
		test 1.V=1.R#1.V
		mat A_`i'[4,1] = r(p)
		test 1.R=1.R#1.V
		mat A_`i'[5,1] = r(p)
		mat list A_`i'
		matmap A_`i'  AN_`i', map(round(@,0.01))
		mat list AN_`i'
		
	areg fert_gps_acre_ i.V##i.R fert_gps_baseline if year==201`i' & /// 
		fert_kg_acre_!=. &fert_kgacre_baseline!=., a(village) robust cl(vil) 
			outreg2 using "$path/replication/output/TableA13.doc", append ///
			ctitle(`i' kggps) bd(2) sd(2) nocons addtext(Village FE, YES) /// 
			keep(1.V 1.R 1.V#1.R fert_gps_baseline)
	    su fert_gps_baseline if e(sample) & trt2==1
		mat B_`i'[1,1] = r(mean)
		mat B_`i'[2,1] = r(sd)
		test 1.V=1.R
		mat B_`i'[3,1] = r(p)  
		test 1.V=1.R#1.V
		mat B_`i'[4,1] = r(p)
		test 1.R=1.R#1.V
		mat B_`i'[5,1] = r(p)
		mat list B_`i'
		matmap B_`i'  BN_`i', map(round(@,0.01))
		mat list BN_`i'		

	areg fert_dum_ i.V##i.R fert_dum_baseline if year==201`i'  & e(sample), a(village) robust cl(vil) 
			outreg2 using "$path/replication/output/TableA13.doc", append ///
			ctitle(`i' dum) bd(2) sd(2) nocons addtext(Village FE, YES)	 /// 
			keep(1.V 1.R 1.V#1.R fert_dum_baseline)
		su fert_dum_ if e(sample) & trt2==1
		mat C_`i'[1,1] = r(mean)
		mat C_`i'[2,1] = r(sd)
		test 1.V=1.R
		mat C_`i'[3,1] = r(p)  
		test 1.V=1.R#1.V
		mat C_`i'[4,1] = r(p)
		test 1.R=1.R#1.V
		mat C_`i'[5,1] = r(p)
		mat list C_`i'
		matmap C_`i'  CN_`i', map(round(@,0.01))
		mat list CN_`i'	
	}
	
	erase "$path/replication/output/TableA13.txt"
	
	*Export											
	matmap A_6 A_6N, map(round(@,0.01))											
	matmap A_9 A_9N, map(round(@,0.01))											
	matmap B_6 B_6N, map(round(@,0.01))											
	matmap B_9 B_9N, map(round(@,0.01))											
	matmap C_6 C_6N, map(round(@,0.01))											
	matmap C_9 C_9N, map(round(@,0.01))											
	
	putexcel set "$path/replication/output/TableA13_F_tests.xls", sheet("F-tests") replace
	putexcel B1 = "fert_kg_acre_16"
	putexcel C1 = "fert_gps_acre_16"
	putexcel D1 = "fert_dum_16"
	putexcel E1 = "fert_kg_acre_19"
	putexcel F1 = "fert_gps_acre_19"
	putexcel G1 = "fert_dum_19"
	putexcel A2 = "CONTROL MEAN"
	putexcel A3 = "CONTROL SD"
	putexcel A4 = "V_vs_R"
	putexcel A5 = "V_vs_RV"
	putexcel A6 = "R_vs_RV"
	
	putexcel B2=matrix(AN_6) 											
	putexcel C2=matrix(BN_6)											
	putexcel D2=matrix(CN_6) 											
	putexcel E2=matrix(AN_9) 											
	putexcel F2=matrix(BN_9)											
	putexcel G2=matrix(CN_9) 

end


program CrossVillageITT

	mat A_6 = J(5,1,.) 
	mat A_9 = J(5,1,.) 
	mat B_6 = J(5,1,.) 
	mat B_9 = J(5,1,.) 
	mat C_6 = J(5,1,.) 
	mat C_9 = J(5,1,.) 
	mat D_6 = J(5,1,.) 
	mat D_9 = J(5,1,.) 
	mat E_6 = J(5,1,.) 
	mat E_9 = J(5,1,.) 
	mat F_6 = J(5,1,.) 
	mat F_9 = J(5,1,.)
	
	forval i=6/9 {
	if inlist(`i', 7, 8) continue
		xtreg fert_kg_acre_ i.trt2 fert_kgacre_baseline if year==201`i' & /// 
		fert_gps_acre_!=. &fert_gps_baseline!=., fe robust cl(vil) 
			outreg2 using "$path/replication/output/TableA16.doc", append ///
			ctitle(`i' kgacre) bd(2) sd(2) nocons addtext(Village FE, YES)
		su fert_kgacre_baseline if e(sample) & trt2==1
		mat A_`i'[1,1] = r(mean)
		mat A_`i'[2,1] = r(sd)
		test 2.trt2=3.trt2
		mat A_`i'[3,1] = r(p)  
		test 2.trt2=4.trt2
		mat A_`i'[4,1] = r(p)
		test 3.trt2=4.trt2
		mat A_`i'[5,1] = r(p)
		mat list A_`i'
		matmap A_`i'  AN_`i', map(round(@,0.01))
		mat list AN_`i'
		
		xtreg fert_gps_acre_ i.trt2 fert_gps_baseline if year==201`i' & /// 
		fert_kg_acre_!=. &fert_kgacre_baseline!=., fe robust cl(vil) 
			outreg2 using "$path/replication/output/TableA16.doc", append ///
			ctitle(`i' kggps) bd(2) sd(2) nocons addtext(Village FE, YES)
	    su fert_gps_baseline if e(sample) & trt2==1
		mat B_`i'[1,1] = r(mean)
		mat B_`i'[2,1] = r(sd)
		test 2.trt2=3.trt2
		mat B_`i'[3,1] = r(p)  
		test 2.trt2=4.trt2
		mat B_`i'[4,1] = r(p)
		test 3.trt2=4.trt2
		mat B_`i'[5,1] = r(p)
		mat list B_`i'
		matmap B_`i'  BN_`i', map(round(@,0.01))
		mat list BN_`i'		

		xtreg fert_dum_ i.trt2  fert_dum_baseline if year==201`i' & e(sample), fe robust cl(vil) 
			outreg2 using "$path/replication/output/TableA16.doc", append ///
			ctitle(`i' dum) bd(2) sd(2) nocons addtext(Village FE, YES)	
		su fert_dum_ if e(sample) & trt2==1
		mat C_`i'[1,1] = r(mean)
		mat C_`i'[2,1] = r(sd)
		test 2.trt2=3.trt2
		mat C_`i'[3,1] = r(p)  
		test 2.trt2=4.trt2
		mat C_`i'[4,1] = r(p)
		test 3.trt2=4.trt2
		mat C_`i'[5,1] = r(p)
		mat list C_`i'
		matmap C_`i'  CN_`i', map(round(@,0.01))
		mat list CN_`i'	
	}
	erase "$path/replication/output/TableA16.txt"

	putexcel set "$path/replication/output/TableA16_F_tests.xls", sheet("F-tests") replace
	
	putexcel B2=matrix(AN_6) 											
	putexcel C2=matrix(BN_6)											
	putexcel D2=matrix(CN_6) 											
	putexcel E2=matrix(AN_9) 											
	putexcel F2=matrix(BN_9)											
	putexcel G2=matrix(CN_9) 
	
	putexcel B1 = "fert_kg_acre_16"
	putexcel C1 = "fert_gps_acre_16"
	putexcel D1 = "fert_dum_16"
	putexcel E1 = "fert_kg_acre_19"
	putexcel F1 = "fert_gps_acre_19"
	putexcel G1 = "fert_dum_19"
	putexcel A2 = "CONTROL MEAN"
	putexcel A3 = "CONTROL SD"
	putexcel A4 = "V_vs_R"
	putexcel A5 = "V_vs_RV"
	putexcel A6 = "R_vs_RV"
		
end

	
program pooling

	gen trt3=(trt2==2 | trt2==4)
	mat A_6 = J(2,1,.) 											
	mat A_9 = J(2,1,.) 											
	mat B_6 = J(2,1,.) 											
	mat B_9 = J(2,1,.) 											
	mat C_6 = J(2,1,.) 											
	mat C_9 = J(2,1,.) 											
	mat D_6 = J(2,1,.) 											
	mat D_9 = J(2,1,.) 	
	mat E_6 = J(2,1,.) 
	mat E_9 = J(2,1,.) 
	mat F_6 = J(2,1,.) 
	mat F_9 = J(2,1,.)
																				
	forval i=6/9 {			
	if inlist(`i', 7, 8) continue
		xtreg fert_kg_acre_ i.trt3 fert_kgacre_baseline if year==201`i' & /// 
		fert_gps_acre_!=. &fert_gps_baseline!=., fe robust cl(vil) 		
			outreg2 using "$path/replication/output/TableA7.doc", append ///	
			ctitle(`i' kgacre) bd(2) sd(2) nocons addtext(Village FE, YES)
        su fert_kgacre_baseline if e(sample) & trt3==0
		mat A_`i'[1,1] = r(mean)	
		mat A_`i'[2,1] = r(sd)		
		mat list A_`i'
		
		xtreg fert_gps_acre_ i.trt3 fert_gps_baseline if year==201`i' & /// 
		fert_kg_acre_!=. &fert_kgacre_baseline!=., fe robust cl(vil) 		
			outreg2 using "$path/replication/output/TableA7.doc", append ///	
			ctitle(`i' kgGPS) bd(2) sd(2) nocons addtext(Village FE, YES)
        su fert_gps_baseline if e(sample) & trt3==0
		mat B_`i'[1,1] = r(mean)	
		mat B_`i'[2,1] = r(sd)		
		mat list B_`i'
		
		xtreg fert_dum_ i.trt3  fert_dum_baseline if year==201`i' & e(sample), fe robust cl(vil) 		
			outreg2 using "$path/replication/output/TableA7.doc", append ///	
			ctitle(`i' dum) bd(2) sd(2) nocons addtext(Village FE, YES)	
        su fert_dum_baseline if e(sample) & trt3==0
		mat C_`i'[1,1] = r(mean)	
		mat C_`i'[2,1] = r(sd)	
		mat list C_`i'
	}		
	erase "$path/replication/output/TableA7.txt"
	
	*Export											
	matmap A_6 A_6N, map(round(@,0.01))											
	matmap A_9 A_9N, map(round(@,0.01))											
	matmap B_6 B_6N, map(round(@,0.01))											
	matmap B_9 B_9N, map(round(@,0.01))											
	matmap C_6 C_6N, map(round(@,0.01))											
	matmap C_9 C_9N, map(round(@,0.01))											
	
	putexcel set "$path/replication/output/TableA7_F_tests.xls", sheet("F-tests") replace	
	putexcel B1 = "fert_kg_acre_16"
	putexcel C1 = "fert_gps_acre_16"
	putexcel D1 = "fert_dum_16"
	putexcel E1 = "fert_kg_acre_19"
	putexcel F1 = "fert_gps_acre_19"
	putexcel G1 = "fert_dum_19"
	putexcel A2 = "CONTROL MEAN"
	putexcel A3 = "CONTROL SD"
	
	putexcel B2=matrix(A_6N) 											
	putexcel C2=matrix(B_6N)
	putexcel D2=matrix(C_6N) 
	putexcel E2=matrix(A_9N) 											
	putexcel F2=matrix(B_9N)
	putexcel G2=matrix(C_9N) 	
	
end


program HeterogeneityEC_IPW

	mat A_6 = J(5,1,.)
	mat B_6 = J(5,1,.)
	mat C_6 = J(5,1,.)
	mat D_6 = J(3,1,.)
	mat E_6 = J(3,1,.)
	mat F_6 = J(3,1,.)

	gl het good_EC

	foreach x in $het {
	forval i=6/6 {

	areg fert_kg_acre_ i.trt2##c.`x' fert_kgacre_baseline if fert_gps_acre_!=. ///
	                             & fert_gps_baseline!=. & year==201`i' [aw=ipw], a(village) r cl(village)
	su fert_kgacre_baseline if e(sample) & trt2==1 
		mat A_`i'[1,1] = r(mean)
		mat A_`i'[2,1] = r(sd)
		test 2.trt2=3.trt2
		mat A_`i'[3,1] = r(p)
		test 2.trt2=4.trt2
		mat A_`i'[4,1] = r(p)
		test 3.trt2=4.trt2
		mat A_`i'[5,1] = r(p)
		
		test 2.trt2 + 2.t#c.`x' = 3.trt2 + 3.t#c.`x'
		mat D_`i'[1,1] = r(p)
		test 2.trt2 + 2.t#c.`x' = 4.trt2 + 4.t#c.`x'
		mat D_`i'[2,1] = r(p)
		test 3.trt2 + 3.t#c.`x' = 4.trt2 + 4.t#c.`x'
		mat D_`i'[3,1] = r(p)

	outreg2 using "$path/replication/output/Table8_A.doc", ///
		append ctitle(SR1`i') bd(3) sd(3) nocons label addtext(Village FE, YES) /// 
		keep(2.trt2 3.trt2 4.trt2 fert_gps_baseline fert_dum_baseline fert_kgacre_baseline)
		
	*Marginal
	margins, expression( _b[2.t] + _b[2.t#c.`x']) post
	 outreg2 using "$path/replication/output/Table8_B.xls", ///
		append ctitle(Fert`i'V) bd(3) sd(3)
	qui areg fert_kg_acre_ i.trt2##c.`x' fert_kgacre_baseline if fert_gps_acre_!=. ///
	                             & fert_gps_baseline!=. & year==201`i' [aw=ipw], a(village) r cl(village)
	margins, expression( _b[3.t] + _b[3.t#c.`x']) post
	 outreg2 using "$path/replication/output/Table8_B.xls", ///
		append ctitle(Fert`i'R) bd(3) sd(3)
	qui areg fert_kg_acre_ i.trt2##c.`x' fert_kgacre_baseline if fert_gps_acre_!=. ///
	                             & fert_gps_baseline!=. & year==201`i' [aw=ipw], a(village) r cl(village)
	margins, expression( _b[4.t] + _b[4.t#c.`x']) post
	 outreg2 using "$path/replication/output/Table8_B.xls", ///
		append ctitle(Fert`i'RV) bd(3) sd(3)

	***GPS
	qui areg fert_gps_acre_ i.trt2##c.`x' fert_gps_baseline if year==201`i' & /// 
		fert_kg_acre_!=. &fert_kgacre_baseline!=. [aw=ipw], a(village) r cl(village)
	su fert_gps_baseline if e(sample) & trt2==1 
	mat B_`i'[1,1] = r(mean)
	mat B_`i'[2,1] = r(sd)
	test 2.trt2=3.trt2
	mat B_`i'[3,1] = r(p)
	test 2.trt2=4.trt2
	mat B_`i'[4,1] = r(p)
	test 3.trt2=4.trt2
	mat B_`i'[5,1] = r(p)

	test 2.trt2 + 2.t#c.`x' = 3.trt2 + 3.t#c.`x'
	mat E_`i'[1,1] = r(p)
	test 2.trt2 + 2.t#c.`x' = 4.trt2 + 4.t#c.`x'
	mat E_`i'[2,1] = r(p)
	test 3.trt2 + 3.t#c.`x' = 4.trt2 + 4.t#c.`x'
	mat E_`i'[3,1] = r(p)

	outreg2 using "$path/replication/output/Table8_A.doc", ///
		append ctitle(Sat1`i') nocons bd(3) sd(3) label addtext(Village FE, YES) /// 
		keep(2.trt2 3.trt2 4.trt2 fert_gps_baseline fert_dum_baseline fert_kgacre_baseline)
		
	*Marginal
	margins, expression( _b[2.t] + _b[2.t#c.`x']) post
	 outreg2 using "$path/replication/output/Table8_B.xls", ///
		append ctitle(F_gps`i'V) bd(3) sd(3)
	qui areg fert_gps_acre_ i.trt2##c.`x' fert_gps_baseline if year==201`i' & /// 
		fert_kg_acre_!=. &fert_kgacre_baseline!=. [aw=ipw], a(village) r cl(village)
	margins, expression( _b[3.t] + _b[3.t#c.`x']) post
	 outreg2 using "$path/replication/output/Table8_B.xls", ///
		append ctitle(F_gps`i'R) bd(3) sd(3)
	qui areg fert_gps_acre_ i.trt2##c.`x' fert_gps_baseline if year==201`i' & /// 
		fert_kg_acre_!=. &fert_kgacre_baseline!=. [aw=ipw], a(village) r cl(village)
	margins, expression( _b[4.t] + _b[4.t#c.`x']) post
	 outreg2 using "$path/replication/output/Table8_B.xls", ///
		append ctitle(F_gps`i'RV) bd(3) sd(3)

	***Binary
	qui areg fert_dum_ i.trt2##c.`x' fert_dum_baseline if year==201`i' & e(sample) [aw=ipw], a(village) r cl(village)
	 su fert_dum_baseline if e(sample) & trt2==1 
	 mat C_`i'[1,1] = r(mean)
	 mat C_`i'[2,1] = r(sd)
	 test 2.trt2=3.trt2
	 mat C_`i'[3,1] = r(p)
	 test 2.trt2=4.trt2
	 mat C_`i'[4,1] = r(p)
	 test 3.trt2=4.trt2
	 mat C_`i'[5,1] = r(p)

	 test 2.trt2 + 2.t#c.`x' = 3.trt2 + 3.t#c.`x'
	 mat F_`i'[1,1] = r(p)
	 test 2.trt2 + 2.t#c.`x' = 4.trt2 + 4.t#c.`x'
	 mat F_`i'[2,1] = r(p)
	 test 3.trt2 + 3.t#c.`x' = 4.trt2 + 4.t#c.`x'
	 mat F_`i'[3,1] = r(p)

	outreg2 using "$path/replication/output/Table8_A.doc", ///
		append ctitle(GPS1`i') nocons bd(3) sd(3) label addtext(Village FE, YES) /// 
		keep(2.trt2 3.trt2 4.trt2 fert_gps_baseline fert_dum_baseline fert_kgacre_baseline)
	
	*Marginal								
	margins, expression( _b[2.t] + _b[2.t#c.`x']) post
	 outreg2 using "$path/replication/output/Table8_B.xls", ///
		append ctitle(Binary`i'V) bd(3) sd(3)
	qui areg fert_dum_ i.trt2##c.`x' fert_dum_baseline if year==201`i' & e(sample) [aw=ipw], a(village) r cl(village)

	margins, expression( _b[3.t] + _b[3.t#c.`x']) post
	 outreg2 using "$path/replication/output/Table8_B.xls", ///
		append ctitle(Binary`i'R) bd(3) sd(3)
	qui areg fert_dum_ i.trt2##c.`x' fert_dum_baseline if year==201`i' & e(sample) [aw=ipw], a(village) r cl(village) 

	margins, expression( _b[4.t] + _b[4.t#c.`x']) post
	 outreg2 using "$path/replication/output/Table8_B.xls", ///
		append ctitle(Binary`i'RV) bd(3) sd(3)
	}
	
	*Export
	matmap A_6 A_6N_30, map(round(@,0.01))
	matmap B_6 B_6N_30, map(round(@,0.01))
	matmap C_6 C_6N_30, map(round(@,0.01))

	matmap D_6 D_6N_30, map(round(@,0.01))
	matmap E_6 E_6N_30, map(round(@,0.01))
	matmap F_6 F_6N_30, map(round(@,0.01))

	putexcel set "$path/replication/output/Table8_F_tests.xls", sheet("F-tests") replace
	putexcel B2=matrix(A_6N_30)
	putexcel C2=matrix(B_6N_30)
	putexcel D2=matrix(C_6N_30)

	putexcel B8=matrix(D_6N_30)
	putexcel C8=matrix(E_6N_30)
	putexcel D8=matrix(F_6N_30)
	
	putexcel B1 = "fert_kg_acre_16"
	putexcel C1 = "fert_gps_acre_16"
	putexcel D1 = "fert_dum_16"
	
	putexcel A2 = "CONTROL MEAN"
	putexcel A3 = "CONTROL SD"
	putexcel A4 = "V_vs_R (panel A)"
	putexcel A5 = "V_vs_RV (panel A)"
	putexcel A6 = "R_vs_RV (panel A)"
	
	putexcel A8 = "V_vs_R (panel B)"
	putexcel A9 = "V_vs_RV (panel B)"
	putexcel A10 = "R_vs_RV (panel B)"
	
	}	
	erase "$path/replication/output/Table8_A.txt"
	erase "$path/replication/output/Table8_B.txt"
	
end	

*Execute												
main												
												
*EOF			
	

